// pages/editProfile/editProfile.js
const http = require('../../utils/http.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    school:'',
    year:'',
    major:'',
    gxqm:'',
    zodiac:['白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天瓶座','天蝎座','射手座','摩羯座','水瓶座','双鱼座'],
    zoidx:'',
    sexArr:['男','女'],
    sexidx:'',
    infor:''
  },

  //表单提交
  submitForm(e){
    console.log(e.detail.value);
    var formDetail = e.detail.value
    this.setData({
      formDetail
    })
    var uid = app.globalData.uid,
      bl_school = formDetail.school,
      bl_entrance = formDetail.year,
      bl_specialty = formDetail.major,
      bl_age = formDetail.age || 0,
      bl_xingzuo = this.data.zodiac[formDetail.constellation] || 0,
      bl_gender = this.data.sexArr[formDetail.sex] == '男' ? '1' : this.data.sexArr[formDetail.sex] == '女'?'2':0,
      bl_gxqm = formDetail.autograph || 0
    http.fetchApi('procedure_t', { p: `R2021003|${uid}|${bl_school}|${bl_entrance}|${bl_specialty}|${bl_age}|${bl_xingzuo}|${bl_gender}|${bl_gxqm}` }).then(res=>{
      if(res.data.code==1){
        wx.showToast({
          title: '保存成功',
        })
        setTimeout(()=>{
          wx.navigateBack({
            
          })
        },1000)
      }else{
        wx.showToast({
          title: '服务器异常',
          icon:'none'
        })
      }
    })
  },

  //选择性别
  bindPickerChange(e){
    this.setData({
      sexidx: e.detail.value
    })
  },

  //星座
  changeXingzuo(e){
    this.setData({
      zoidx: e.detail.value
    })
  },

//大学名称
  selectSchool(e){
    this.setData({
      school:e.detail.value
    })
  },
  selectYear(e){
    this.setData({
      year: e.detail.value
    })
  },
  selectMajor(e){
    this.setData({
      major: e.detail.value
    })
  },

  // 个人资料展示
  showInfor(){
    var uid = app.globalData.uid
    http.fetchApi('procedure_t', { p: `R2019002|${uid}|${uid}` }).then(res=>{
      if(res.data.code==1){
        var infor = res.data.results[0];
        var zoidx = this.data.zodiac.findIndex(item => item == infor.xingzuo),
          sexidx = this.data.sexArr.findIndex(item => item == infor.xb)
        this.setData({
          infor,
          school: infor.school,
          year: infor.entrance,
          major: infor.faculty,
          gxqm: infor.gxqm,
           zoidx,
          sexidx
        })
      }else{
        wx.showToast({
          title: '服务器异常',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: app.globalData.userInfo
    })
    this.showInfor();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})